function [indp,con,calpr,prior,alphap,betap]=loadprior(dist,lcone,lctwo,lowerb,upperb)
% =========================================================================
% 
% LOADPRIOR.m 
%
% Loads the prior for the coefficients and checks that 
% the calibrated parameters have been set properly  
%
% function [indp,con,calpr,alphap,betap]=loadprior(dist,lcone,lctwo); 
%
%% Inputs 
% DIST     [Np 1] Cell array, Name of the densities for the full
%           parameter vector 
%           e.g. N (Normal), W (Wishart), B (Beta), I (Inverse Wishart), 
%                G (Gamma),  U (Uniform), E (Weibull), T (Truncated Beta) 
%                C (Calibrated) 
% LCONE     [Np 1] Vector, Location Parameter 1, e.g. Mean or Alpha
%
% LCTWO     [Np 1] Vector, Location Parameter 2, e.g. Std. Dev. or Beta 
%
% LOWERB    [Np 1] Vector, Lower Bound for the estimation 
%
% UPPERB    [Np 1] Vector, Upper Bound for the estimation  
%
%% Output
% Partition [Np 1] vector into Ne and Nc positions 
%
% indp       [Ne 1] Vector, Position indicator of estimated parameters 
% 
% con        [Nc  1] Vector, Position of parameters calibrated 
% 
% calpr      [Nc  1] Vector, calibrated parameter 
% 
% alphap     [Ne  1] ALPHA for the computation of the priors 
% betap      [Ne  1] BETA  for the computation of the priors
% 
% For the UNIFORM  ALPHAP and BETAP are given by ALPHAP and BETAP 
%
% AJ 6/25/03 
% Modified 4/27/2007 
% 1/13/2009 Add new density T which is a truncated beta. 
% See MSDBETAB. for details. Bounds of the Uniform defined in MSDTOAB.m 
% 
% =================================================================
lcone=lcone(:); lctwo=lctwo(:); lowerb=lowerb(:); upperb=upperb(:); 
if length(lcone)~=length(lowerb); 
    error('Mistmatch LOC and BND matrices') 
end 
nump=size(lcone,1); 
dist=char(dist); 

% prior : Matrix handling all prior information 
%=============================================
nor=find(dist =='N'); numn=length(nor);  
iwi=find(dist =='W'); numw=length(iwi); 
gam=find(dist =='G'); numg=length(gam); 
bet=find(dist =='B'); numb=length(bet); 
con=find(dist =='C'); numc=length(con); %prior(1:numc,5)=con;
ig= find(dist =='I'); numi=length(ig); 
uni=find(dist =='U'); numu=length(uni); 
utr=find(dist =='T'); numt=length(utr);
weibull=find(dist =='E'); nume=length(weibull);

if nargin < 4 
    disp('No Uniform allowed called with 3 arguments') 
    if uni > 1 
        error('Need to pass LOWER BOUND AND UPPER BOUND FOR UNIFORM') 
    end 
end 

%% Matrix with the positions of each estimated parameters, per distribution 
prone=[numn numw numg numb numi numu numt nume];
prior=zeros(max(prone),8);
prior(1:numn,1)=nor;
prior(1:numw,2)=iwi;
prior(1:numg,3)=gam;
prior(1:numb,4)=bet;
prior(1:numi,5)=ig;
prior(1:numu,6)=uni;
prior(1:numt,7)=utr;
prior(1:nume,8)=weibull;
 
%% First row is number of elements per column 
prior=[prone;prior]; clear prone;

% =========================================================
%       Indicator of parameters to estimate 
% ==========================================================
indp=(1:1:nump)'; 
if ~isempty(con);
    indp(con)=[];
end
% ===============================================================
%       Check calibrated parameters specified properly 
% ===============================================================
calpr=lcone(con);
if ~isempty(con); 
    ch1=any( lctwo(con) ~= 0 ); 
    if sum(ch1) > 0;
        ch1
        error('Fourth Column of prior should be set zero for calibrated parameter');  
    end; clear ch1; 
end; 
clear ch1;   

% ========================================================
%         Find ALPHA & BETA per distribution
%         -----------------------------------
%
% Convert location parameters of the non-calibrated 
% coefficients into alpha & beta 
% ==========================================================
if sum ( any( lctwo(indp) == 0 ) ) > 0; 
    error( 'Non-calibrated parameters must have positive variance'); 
end; 

% ==============================================
% Fill in values for the calibrated parameters 
% ==============================================
% With the T density need to pass the Lower and Upper Bounds 
alphap=zeros(nump,1); 
betap =zeros(nump,1); 
[alphatemp,betatemp]=msdtoab(lcone(indp),lctwo(indp),dist(indp),lowerb(indp),upperb(indp)); 
alphap(indp)=alphatemp; 
betap(indp) =betatemp;
% % Replace the uniform with the proper bounds....
% % alphap(uni)=lowerb(uni); 
% % betap(uni)=upperb(uni); 
% % 1/13/09 This is done inside the code msdtoab now  
% clear alphav betav; 